package com.jianzhile.jobs.fragments;

import android.app.Fragment;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;

import com.jianzhile.jobs.R;
import com.jianzhile.jobs.activities.MainActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


public class HomeFragment extends Fragment implements View.OnClickListener{

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    @Bind(R.id.fragment_home_pager)
    public ViewPager pager;

    @Bind(R.id.fragment_home_work_pager)
    public ViewPager workPager;

    @Bind(R.id.fragment_home_list)
    ListView listView;
    @Bind(R.id.fragment_home_sv)
    ScrollView scrollView;
    @Bind(R.id.fragment_home_you_may_like)
    public LinearLayout youLikeLl;
    @Bind(R.id.fragment_home_high_pay_ll)
    public LinearLayout highpayLl;
    @Bind(R.id.fragment_home_company_ll)
    public LinearLayout companyLl;
    @Bind(R.id.fragment_home_local_ll)
    public LinearLayout localLl;
    public HomeFragment() {
    }


    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);
        scrollView.smoothScrollTo(0, 0);
        return view;
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initWorkPageView();
        initListener();
        pager.setOffscreenPageLimit(3);
        pager.setAdapter(new MyPagerAdapter());
        workPager.setAdapter(new MyWorkPagerAdapter());
        workPager.setPageMargin(-20);

        workPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        workPager.setCurrentItem(2);
        //listView.addHeaderView(LayoutInflater.from(getActivity()).inflate(R.layout.fragment_home_list_header, null));
        listView.addFooterView(LayoutInflater.from(getActivity()).inflate(R.layout.fragment_home_list_footer, null));
        listView.setAdapter(new MyListAdapter());
        listView.setDivider(new ColorDrawable(getResources().getColor(R.color.white)));
        setListViewHeightBasedOnChildren(listView);
    }


    private void initListener(){
        youLikeLl.setOnClickListener(this);
        highpayLl.setOnClickListener(this);
        companyLl.setOnClickListener(this);
        localLl.setOnClickListener(this);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;

        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onResume() {
        super.onResume();

    }


    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fragment_home_you_may_like:
                ((MainActivity)getActivity()).chooseJobByKeyWord(0);
                break;
            case R.id.fragment_home_high_pay_ll:
                ((MainActivity)getActivity()).chooseJobByKeyWord(1);
                break;
            case R.id.fragment_home_company_ll:
                ((MainActivity)getActivity()).chooseJobByKeyWord(2);
                break;
            case R.id.fragment_home_local_ll:
                ((MainActivity)getActivity()).chooseJobByKeyWord(3);
                break;
        }
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);

        void initToolBar(int tab);
    }


    public void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }


    class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.home_pager_item, null);
            container.addView(view);
            return view;
        }
    }

    List<View> views = new ArrayList<>();
    public void initWorkPageView(){
        for (int i = 0; i < 5; i++){
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.job_item, null);
            views.add(view);
        }
    }
    class MyWorkPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(views.get(position % views.size()));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            container.addView(views.get(position % views.size()), 0);
            return views.get(position % views.size());
        }
    }

    class MyListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return 20;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.job_item_3, null);
                new ViewHolder(convertView);
            }
            ViewHolder holder = (ViewHolder) convertView.getTag();


            return convertView;
        }


        class ViewHolder {

            LinearLayout meauLl;
            ImageView cover;

            public ViewHolder(View view) {

                view.setTag(this);
            }


        }
    }
}
